package cn.com.common.tools;

import java.io.IOException;
import java.util.Iterator;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.google.gson.Gson;

public class JsonUtil {
	private static final Gson gson = new Gson();

	/**
	 * object to json
	 * @param obj
	 * @return json string
	 */
	public static <T> String ObjToJson(T obj) {
		if (obj == null) {
			return "";
		}
		// Gson gson = new Gson();
		String jsonstr = gson.toJson(obj);
		return jsonstr;
	}

	/**
	 * json to object
	 * @param jsonStr
	 * @param c
	 * @return object
	 * @throws InstantiationException
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static <T> T JsonToObj(String jsonStr, Class<? extends T> c)
			throws InstantiationException, Exception {
		// Gson gson = new Gson();
		Object obj = gson.fromJson(jsonStr, c);
		T t = c.newInstance();
		if (c.isInstance(obj)) {
			t = (T) obj;
		}
		return t;
	}

	/**
	 * object to json
	 * @param obj
	 * @return json string
	 */
	public static <T> String MapObjToJson(T obj) {
		if (obj == null) {
			return "";
		}
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
		 
		String userMapJson = "";
		try {
			userMapJson = objectMapper.writeValueAsString(obj);
		} catch (Exception e) {
			return "";
		}
		return userMapJson;
	}

	/**
	 * json to object
	 * @param jsonStr
	 * @param c
	 * @return object
	 * @throws InstantiationException
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static <T> T MapJsonToObj(String jsonStr, Class<? extends T> c)
			throws InstantiationException, Exception {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
		Object obj = objectMapper.readValue(jsonStr, c);
		T t = c.newInstance();
		if (c.isInstance(obj)) {
			t = (T) obj;
		}
		return t;
	}

	/**
	 * get json value
	 * @param jsonstr
	 * @param indexName  json name
	 * @return value result
	 */
	public static String getNodevalue(String jsonstr, String indexName) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			JsonNode jnode = objectMapper.readTree(jsonstr);
			return jnode.path(indexName).asText();
		} catch (JsonProcessingException e) {
		} catch (IOException e) {
		}
		return "";
	}

	/**
	 * deep find json value
	 * @param jsonstr
	 * @param indexName
	 * @return json value
	 */
	public static String getdeepNodevalue(String jsonstr, String indexName) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			JsonNode jnode = objectMapper.readTree(jsonstr);
			return jnode.findValue(indexName).asText();
		} catch (JsonProcessingException e) {
		} catch (IOException e) {
		}
		return "";
	}

	/**
	 * find json value ignorecases
	 * @param jsonstr
	 * @param indexName
	 * @return json value
	 */
	public static String getValueIgnoreCase(String jsonstr, String indexName) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			String result = "";
			JsonNode jnode = objectMapper.readTree(jsonstr);
			Iterator<String> keys = jnode.getFieldNames();
			while (keys.hasNext()) {
				String fieldName = keys.next();
				if (fieldName.equalsIgnoreCase(indexName)) {
					result = jnode.path(fieldName).asText();
					break;
				}
			}
			return result;
		} catch (JsonProcessingException e) {
		} catch (IOException e) {
		}
		return "";
	}
	public static String getJsonnodestr(String jsonstr, String indexName){
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			JsonNode jnode = objectMapper.readTree(jsonstr);
			return jnode.get(indexName).toString();
		} catch (JsonProcessingException e) {
		} catch (IOException e) {
		}
		return "";
		
	}
}
